<?php
$link = explode('&',$_SERVER['REQUEST_URI']);
$total = count($link);
if($total>1){
	for($i=1; $i<$total; $i++){
		$m = explode('=',$link[$i]);
		$type = $m[0];
		$value = $m[1];
		if($type=='nm'){
			$nm = $value;
			$qr = mysql_query("SELECT id FROM menu WHERE `delete`=0 AND status=1 AND name_rewrite='{$nm}'");
			$row = mysql_fetch_array($qr);
			$qr_nm = "AND menu_id='{$row['id']}'";
		}elseif($type=='ls'){
			$ls = $value;
			$qr = mysql_query("SELECT id FROM sim_type WHERE `delete`=0 AND status=1 AND name_rewrite='{$ls}'");
			$row = mysql_fetch_array($qr);
			$qr_ls = "AND sim_type='{$row['id']}'";
		}elseif($type=='gt'){
			$gt = $value;
			$qr_gt = "AND price>='{$gt}'";
		}elseif($type=='gd'){
			$gd = $value;
			$qr_gd = "AND price<='{$gd}'";
		}elseif($type=='sb'){
			if($value==0){
				$sb = 'ASC';
				$sb_0 = 'selected="selected"';
			}else{
				$sb = 'DESC';
				$sb_1 = 'selected="selected"';
			}
		}
	}
}

$from = (($page - 1) * max_sim_so);
$select = 'name,price';
$table = 'sim';
$where = "`delete`=0 AND status=1 {$qr_nm} {$qr_ls} {$qr_gt} {$qr_gd}";
$limit = "LIMIT {$from},".max_sim_so;
$list = mysql_query("SELECT sim.id,sim.name,price,menu.name as nhamang,sim_type.name as loai_sim FROM sim,menu,sim_type WHERE sim.`delete`=0 AND sim.status=1 AND menu_id=menu.id AND sim_type=sim_type.id {$qr_nm} {$qr_ls} {$qr_gt} {$qr_gd} ORDER BY price {$sb} {$limit}");
$total = mysql_num_rows($list);
if($total>0){
	while($row = mysql_fetch_array($list)){
		$view_data .= "<tr><th id='simso_{$row['id']}'>{$row['name']}</th> <td align='center' id='giasim_{$row['id']}'>".number_format($row['price'],0,',','.')."đ</td>
		<td style='color:#0072FF; text-align:center'>{$row['nhamang']}</td> <td style='color:#0072FF; text-align:center'>{$row['loai_sim']}</td>
		<td align='center' style='color:#FF8A00; cursor:pointer' class='datmuasim datmuasim_{$row['id']}'>Đặt mua</td></tr>";
	}
}else $view_data = "<tr><th colspan='5' style='color:#0072FF'>Không có thông tin bạn muốn tìm..</th></tr>";

echo '<div id="navigator"><a href=""><img src="images/home.png" alt="Thiên Long mobile" /></a>'.$tc->navigator($row_menu_one['id']).'</div>
<div id="home_product" style="width:1000px">';
$view_post .= '<h1>'.$row_menu_one['title'].'</h1><h2>'.$row_menu_one['metaDescription'].'</h2>';

$qr = mysql_query("SELECT id,name,name_rewrite FROM menu WHERE `delete`=0 AND status=1 AND type_id=6 AND parent_id<>0");
while($row = mysql_fetch_array($qr)){
	if($nm!=$row['name_rewrite']) $nhamang .= '<option value="'.$row['name_rewrite'].'">'.$row['name'].'</option>';
	else $nhamang .= '<option value="'.$row['name_rewrite'].'" selected="selected">'.$row['name'].'</option>';
}

$qr = mysql_query("SELECT id,name,name_rewrite FROM sim_type WHERE `delete`=0 AND status=1 AND id<>0 ORDER BY name");
while($row = mysql_fetch_array($qr)){
	if($ls!=$row['name_rewrite']) $loaisim .= '<option value="'.$row['name_rewrite'].'">'.$row['name'].'</option>';
	else $loaisim .= '<option value="'.$row['name_rewrite'].'" selected="selected">'.$row['name'].'</option>';
}

$array_giatu = array('50000','150000','250000','500000','1000000','2000000','4000000','6000000');
for($i=0; $i<count($array_giatu); $i++){
	if($gt!=$array_giatu[$i]) $giatu .= '<option value="'.$array_giatu[$i].'">'.number_format($array_giatu[$i],0,',','.').'</option>';
	else $giatu .= '<option value="'.$array_giatu[$i].'" selected="selected">'.number_format($array_giatu[$i],0,',','.').'</option>';
}

$array_gianden = array('150000','250000','500000','1000000','2000000','4000000','6000000','10000000');
for($i=0; $i<count($array_giatu); $i++){
	if($gd!=$array_gianden[$i]) $giaden .= '<option value="'.$array_gianden[$i].'">'.number_format($array_gianden[$i],0,',','.').'</option>';
	else $giaden .= '<option value="'.$array_gianden[$i].'" selected="selected">'.number_format($array_gianden[$i],0,',','.').'</option>';
}

$view_post .= '
<div id="timkiem_sim">
	<p style="width:auto; float:left">Tìm theo: 
	<select name="nhamang"><option value="">-- Mạng --</option>'.$nhamang.'</select>
	<select name="loaisim"><option value="">-- Loại SIM --</option>'.$loaisim.'</select>
	<select name="giatu"><option value="">-- Giá từ --</option>'.$giatu.'</select>
	<select name="giaden"><option value="">-- Giá đến --</option>'.$giaden.'</select>
	</p>
	<p style="width:auto; float:right">Xếp theo: <select name="sortby"><option value="0" '.$sb_0.'>Giá tăng dần</option> <option value="1" '.$sb_1.'>Giá giảm dần</option></select></p>
</div>
<table width="100%" border="0" cellpadding="0" cellspacing="0" id="sim_so">
<tr bgcolor="#f2f2f2"><th>SIM số</th> <th>Giá bán</th> <th>Mạng</th> <th>Loại SIM</th> <th>Đặt mua</th></tr>
'.$view_data.'</table>';

$other_link = explode('/?',$_SERVER['REQUEST_URI']);
$phantrang = $tc->phantrang($row_menu_one['url'],$page,max_sim_so,'id',$table,$where,'?'.$other_link[1]);
$view_post .= '<div style="clear:both; height:1px"></div><div id="phantrang">'.$phantrang.'<div style="clear:both; height:1px"></div></div>';
echo $view_post.'</div>';
?>
<div id="popupContact">
	<div style="width:950px;height:30px; float:right; text-align:right; position:absolute; z-index:1300;">
		<img src="images/close.png" alt="close" class="popupContactClose" />
	</div>
	<div style="clear:both; height:15px"></div>
	<div style="clear:both; height:15px; background-color:#FFF"></div>
	
	<div id="content_popup"><img src="images/loading.gif" alt="loading" /></div>
</div>
<div id="backgroundPopup"></div>

<script type="text/javascript">
$(document).ready(function(){
	$(".popupContactClose").live("click",function(){
		disablePopup();
	});
	
	/*mua sim*/
	$(".datmuasim").click(function(){
		var id = ($(this).attr("class")).split("datmuasim_");
		var name = $("#simso_" + id[1]).html();
		var price = $("#giasim_" + id[1]).html();
		$.post("ajax.php",{view_datmua_sim:1,id:id[1],name:name,price:price},function(data){
			$("#content_popup").html(data);
			sroll_top();
			centerPopup("fix");
			loadPopup();
			$(window).bind("resize", function(){ centerPopup("fix"); });
			return true;
		});
	});
	$("input[name=submit_muasim]").live("click",function(){
		var id = $("#id_sim").html();
		var name = $("input[name=name]").val();
		var email = $("input[name=email]").val();
		var phone = $("input[name=phone]").val();
		var diachi = $("input[name=diachi]").val();
		var content = $("textarea[name=content]").val();
		if(name.length<3){
			alert("Họ tên phải hơn 3 ký tự");
			$("input[name=name]").focus();
			return false;
		}else if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email))){
			alert("Email chưa đúng");
			$("input[name=email]").focus();
			return false;
		}else if(phone.length<10){
			alert("Điện thoại chưa đúng");
			$("input[name=phone]").focus();
			return false;
		}else if(diachi.length<10){
			alert("Địa chỉ hơn 10 ký tự");
			$("input[name=diachi]").focus();
			return false;
		}else{
			$("#form_lienhe").html('<p style="font-weight:bold; padding:30px">Đang xử lý...</p>');
			$.post("ajax.php",{dathang_sim:1,name:name,email:email,phone:phone,diachi:diachi,content:content,id:id},function(data){
				if(data!="0"){
					$("#form_lienhe").html('<p style="color:#0054B1; font-weight:bold; padding:30px 0 60px;">Đặt hàng thành công, sau 5 giây tự động đóng hộp thoại</p>');
					setTimeout(function(){ disablePopup(); },5000);
				}else $("#form_lienhe").html('<p style="color:#F00; font-weight:bold; padding:30px 0 60px;">Không gửi được. Vui lòng ấn F5 thử lại.</p>');
			});
			return true;
		}
	});
	
	/*tim kiem SIM*/
	function link_default(){
		var nhamang = $("select[name=nhamang]").val();
		var loaisim = $("select[name=loaisim]").val();
		var giatu = $("select[name=giatu]").val();
		var giaden = $("select[name=giaden]").val();
		var sortby = $("select[name=sortby]").val();
		
		if(nhamang!='') nhamang = '&nm=' + nhamang; else nhamang = '';
		if(loaisim!='') loaisim = '&ls=' + loaisim; else loaisim = '';
		if(giatu!='') giatu = '&gt=' + giatu; else giatu = '';
		if(giaden!='') giaden = '&gd=' + giaden; else giaden = '';
		if(sortby!='') sortby = '&sb=' + sortby; else sortby = '';
		return "http://<?php echo $domain.'/'.$row_menu_one['url'];?>1/?" + nhamang + loaisim + giatu + giaden + sortby;
	}
	$("select[name=nhamang], select[name=loaisim], select[name=giatu], select[name=sortby]").change(function(){
		window.location = link_default();
	});
	$("select[name=giaden]").change(function(){
		var giaden = $(this).val();
		var giatu = $("select[name=giatu]").val();
		if(parseInt(giaden)>=parseInt(giatu) || giaden=='') window.location = link_default();
		else{
			alert('Nếu chọn giá đến thì giá đến phải lớn lơn giá từ');
			return false;
		}
	});
});
</script>